/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SistemPenyewaanKamar;

import Com.Checkin_Checkout_Kamar_Case.CheckInForm;
import Com.Checkin_Checkout_Kamar_Case.CheckOutForm;
import Com.Entity.Karyawan;
import Com.HallRenting_Case.HallRentingForm;
import Com.Login_Case.LoginForm;
import Com.MakeReport_Case.DailyReportForm;
import Com.MakeReport_Case.MonthlyReport;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.Timer;

/**
 *
 * @author Yosefina
 */
public class MainController {

    private static javax.swing.JFrame frame;
    private static javax.swing.JDialog dialogLaporan;
    private static javax.swing.JDialog dialogKamar;
    private static javax.swing.JDialog dialogGedung;

    public static javax.swing.JFrame getFrame() {
        return frame;
    }

    public static void setFrame(javax.swing.JFrame frame) {
        MainController.frame = frame;
    }

    public static void clearDialog(){
        dialogKamar = null;
        dialogLaporan = null;
    }

    public static void closeDialog(JDialog dialog){
        clearDialog();
        dialog.dispose();
    }

    public static void openCheckinDialog(javax.swing.JFrame frame) {
        MainController.frame = frame;
        new CheckInForm(frame, true).setVisible(true);
    }

    public static void openCheckOutDialog(javax.swing.JFrame frame) {
        MainController.frame = frame;
        if (MainController.dialogKamar == null) {
            MainController.dialogKamar = new CheckOutForm(frame, false);
            dialogKamar.setVisible(true);
        } else {
            dialogKamar.show();
        }
    }

    public static void openHallRentingDialog(javax.swing.JFrame frame) throws SQLException {
         MainController.frame = frame;
        if (MainController.dialogGedung == null) {
            MainController.dialogGedung = new HallRentingForm(frame, false);
            dialogGedung.setVisible(true);
        } else {
            dialogGedung.show();
        }
    }

    public static void openDailyReportDialog(javax.swing.JFrame frame) {
        MainController.frame = frame;
        new DailyReportForm(frame, true).setVisible(true);

    }

    public static void openMonthlyDialog(javax.swing.JFrame frame) {
        MainController.frame = frame;
        if (MainController.dialogLaporan == null) {
            MainController.dialogLaporan = new MonthlyReport(frame, false);
            dialogLaporan.setVisible(true);
        } else {
            dialogLaporan.show();
        }
    }

    public static void exit() throws Exception {
        Karyawan.logout();
        System.exit(0);
    }

    public static void logout() throws Exception {
        Karyawan.logout();
        getFrame().setVisible(false);
        if (dialogKamar != null) {
            if (dialogKamar.isShowing()) {
                dialogKamar.dispose();
            }
            dialogKamar = null;
        }
        if (dialogLaporan != null) {
            if (dialogLaporan.isShowing()) {
                dialogLaporan.dispose();
            }
            dialogLaporan = null;
        }
        new LoginForm().setVisible(true);
    }

    public static void aturMenu(JLabel label, JMenuItem JMI1, JMenuItem JMI2, JMenuItem JMI3, JMenuItem JMI4) {
        if (label.getText().toUpperCase().matches("ADMINISTRASI")) {
            JMI1.setVisible(false);
            JMI2.setVisible(false);
            JMI3.setVisible(false);
            JMI4.setVisible(true);
        } else {
            JMI1.setVisible(true);
            JMI2.setVisible(true);
            JMI3.setVisible(true);
            JMI4.setVisible(false);
        }
    }

    public static void showDateAndClock(final JLabel label) {
        /**
         *
         * @author Anonim, via search google.com
         */
        ActionListener taskPerformer = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                GregorianCalendar gc = new GregorianCalendar();
                SimpleDateFormat frmtDate = new SimpleDateFormat("dd-MMM-yyyy");

                String nol_jam = "", nol_menit = "", nol_detik = "";

                java.util.Date dateTime = new java.util.Date();
                int nilai_jam = dateTime.getHours();
                int nilai_menit = dateTime.getMinutes();
                int nilai_detik = dateTime.getSeconds();

                if (nilai_jam <= 9) {
                    nol_jam = "0";
                }
                if (nilai_menit <= 9) {
                    nol_menit = "0";
                }
                if (nilai_detik <= 9) {
                    nol_detik = "0";
                }

                String jam = nol_jam + Integer.toString(nilai_jam);
                String menit = nol_menit + Integer.toString(nilai_menit);
                String detik = nol_detik + Integer.toString(nilai_detik);

                label.setText(frmtDate.format(gc.getTime()) + ", " + jam
                        + " : " + menit + " : " + detik + " WIB         ");
            }
        };
        new Timer(1000, taskPerformer).start();
    }

    public static void showKaryawan(JLabel nama, JLabel idKaryawan, JLabel jabatan)
            throws FileNotFoundException, IOException, SQLException, Exception {
        List<Karyawan> kTest = Karyawan.getKoneksiKontrol().getDataKaryawan(Karyawan.getusrID());
        for (Karyawan k : kTest) {
            nama.setText(k.getLastName() + ", " + k.getFirstName());
            idKaryawan.setText(k.getUsername());
            jabatan.setText(k.getJabatan());
        }
    }
}
